﻿@{
    ViewBag.Title = "Index";
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta content="telephone=no" name="format-detection" />
    <!-- 引入YDUI样式 -->
    <link href="~/Content/YDUI/build/css/ydui.css" rel="stylesheet" />
    <!-- 引入YDUI自适应解决方案类库 -->
    <script src="~/Content/YDUI/build/js/ydui.flexible.js"></script>
    <style>
        .m-cell:after { border-bottom-width: 0px; }
        .loginImgeHeader { width: 2rem; height: 2rem; margin: auto; display: block; margin-top: 0.2rem; margin-bottom: 0.5rem; }
        .cell-item { height: 70px; margin-left: 10px; margin-right: 20px; }
        .cell-right { border-bottom: 1px solid #dddcdc; }
    </style>
</head>
<body style="background-color:white;">
    <div class="g-view" id="insParam" data-param="@(ViewBag.insParam)" data-apid="@(ViewBag.apid)">
        <img src="~/Image/logo.png" alt="Alternate Text" class="loginImgeHeader" />
        <div class="m-cell demo-small-pitch">
            <div class="cell-item">
                <div class="cell-right">
                    <input id="insPhone" type="text" class="cell-input" pattern="[0-9]*" placeholder="请输入您的手机号" autocomplete="off">
                    <a href="javascript:;" class="btn btn-warning" id="J_GetCode">获取短信验证码</a>
                </div>
            </div>
            <div class="cell-item">
                <div class="cell-right">
                    <input id="insCode" type="text" class="cell-input" pattern="[0-9]*" placeholder="请输入您的验证码" autocomplete="off">
                </div>
            </div>
            <div class="cell-item">
                <div class="cell-right"><input id="insPwd" type="password" class="cell-input" placeholder="请输入您的密码" autocomplete="off"></div>
            </div>
            <div class="cell-item">
                <div class="cell-right"><input id="insPwdQ" type="password" class="cell-input" placeholder="请再次输入您的密码" autocomplete="off"></div>
            </div>
            <div class="cell-item">
                <div class="cell-right"><input id="insYQR" type="text" class="cell-input" placeholder="邀请人" autocomplete="off" value="邀请人：@(ViewBag.insParam)"></div>
            </div>
        </div>
        <div style="padding:20px"><button onclick="submitFrom()" type="button" class="btn-block btn-danger">注 册</button></div>

    </div>
    <!-- 引入jQuery 2.0+ -->
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <!-- 引入YDUI脚本 -->
    <script src="~/Content/YDUI/build/js/ydui.js"></script>
    <script src="~/Content/JS/md5.js"></script>
    <script>

        var code = "1234";
        !function (win, $) {
            var dialog = win.YDUI.dialog;
            var $getCode = $('#J_GetCode');
            // 定义参数
            $getCode.sendCode({
                disClass: 'btn-disabled', // 禁用按钮样式【必填】
                secs: 60, // 倒计时时长 [可选，默认：60秒]
                run: false,// 是否初始化自动运行 [可选，默认：false]
                runStr: '{%s}秒后重新获取',// 倒计时显示文本 [可选，默认：58秒后重新获取]
                resetStr: '重新获取验证码'// 倒计时结束后按钮显示文本 [可选，默认：重新获取验证码]
            });

            $getCode.on('click', function () {
                var userName = $("#insPhone").val();
                if (userName.length != 11) {
                    YDUI.dialog.toast('请输入正确的手机号', 'error', 1000);
                    return;
                }
                var $this = $(this);


                dialog.loading.open('发送中...');

                var rand = "";
                for (var i = 0; i < 4; i++) {
                    var r = Math.floor(Math.random() * 10);
                    rand += r;
                }
                var insapid = $("#insParam").data("apid");
                $.ajax({
                    url: "https://api.mysubmail.com/message/xsend.json",
                    type: "POST",
                    data: {
                        'appid': insapid,
                        'to':userName,
                        'project': 'i8Ozb3',
                        'signature': '87ec46b26561bdb417b22ee69b321af7',
                        'vars': { 'code': rand }
                    },
                    dataType: "json",
                    success: function (data) {
                        //On ajax success do this
                        console.info("success.");
                        YDUI.dialog.loading.close();
                        $this.sendCode('start');
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        //On error do this
                        console.info("error.");
                        YDUI.dialog.loading.close();
                        $this.sendCode('start');
                    }
                });


                // ajax 成功发送验证码后调用【start】
                //$.get("/Home/sendCode", { "strMobile": userName, "code": code }, function (suc) {
                //    //var smodel = ajaxDone(suc, true);
                //    //if (smodel.flag) {
                //    dialog.loading.close();
                //    $this.sendCode('start');
                //    dialog.toast('已发送', 'success', 1500);
                //    //} else {
                //    //    YDUI.dialog.loading.close();
                //    //    YDUI.dialog.toast('error', smodel.msg);
                //    //}
                //});
            });

        }(window, jQuery);


        function submitFrom() {
            var insPhone = $("#insPhone").val();
            var insCode = $("#insCode").val();
            var insPwd = $("#insPwd").val();
            var insPwdQ = $("#insPwdQ").val();
            var insYQR = $("#insYQR").val();
            var insParam = $("#insParam").data("param");

            if (insPhone.length != 11) {
                YDUI.dialog.toast('请输入正确的手机号', 'error', 1000);
                return;
            }
            if (insCode.length != 4 && insCode != code) {
                YDUI.dialog.toast('请输入正确的验证码', 'error', 1000);
                return;
            }
            if (insPwd.length < 6 || insPwd.length > 15) {
                YDUI.dialog.toast('请输入6-15位密码', 'error', 1000);
                return;
            }
            if (insPwdQ != insPwd) {
                YDUI.dialog.toast('两次输入密码不一致', 'error', 1000);
                return;
            }
            if (insYQR.length == 0) {
                YDUI.dialog.toast('请输入正确的手机号', 'error', 1000);
                return;
            }
            if (insParam.length == 0) {
                YDUI.dialog.toast('请输入正确的邀请人', 'error', 1000);
                return;
            }

            //$.ajax({
            //    url: "http://front.chc.mobi/me/member/signup",
            //    type: "POST",
            //    contentType: "application/json; charset=utf-8",
            //    data: {
            //        'mobile': insPhone,
            //        'passwd': insPwd,
            //        'passwd_repeat': insPwdQ,
            //        'sharemobile': insParam
            //    },
            //    beforeSend: function (request) {
            //        request.setRequestHeader("contentType", "application/x-www-form-urlencoded");
            //        request.setRequestHeader("Access-Control-Allow-Headers", "*");
            //    },
            //    dataType: "json",
            //    success: function (data) {
            //        //On ajax success do this
            //        console.info("success.");
            //        YDUI.dialog.loading.close();
            //    },
            //    error: function (xhr, ajaxOptions, thrownError) {
            //        //On error do this
            //        console.info("error.");
            //        YDUI.dialog.loading.close();
            //    }
            //});

            $.post("/Home/saveRegister", { "insPhone": insPhone, "insCode": insCode, "insPwd": insPwd, "insPwdQ": insPwdQ, "insYQR": insParam }, function (suc) {
                var smodel = JSON.parse(suc);
                if (smodel.responseCode == 200) {
                    YDUI.dialog.toast('注册成功，正在跳转到下载页', 'success', 1000);
                    setTimeout(function () {
                        location.href = "https://www.pgyer.com/xDzG";
                    }, 1000);
                    return;
                }
                YDUI.dialog.toast('用户注册失败！', 'error', 1000);
            });

        }
    </script>
</body>
</html>